package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.databinding.LayoutDialogMonthPickerBinding;

import java.util.Calendar;
import java.util.Locale;

public class MyMonthPicker extends Dialog {

    public ObservableField<String> currentTitle = new ObservableField<>();
    private int currentYear;
    private int currentMonth;
    private OnOkListener onOkListener;

    public void setOnOkListener(OnOkListener onOkListener) {
        this.onOkListener = onOkListener;
    }

    public MyMonthPicker(@NonNull Context context) {
        super(context, R.style.myProgressDialogStyle);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        LayoutDialogMonthPickerBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),R.layout.layout_dialog_month_picker,
                null,false);
        binding.setUtils(this);
        setContentView(binding.getRoot());
        assert getWindow()!=null;
        getWindow().getAttributes().width = (int) (context.getResources().getDisplayMetrics().widthPixels/3.5f);
        Calendar instance = Calendar.getInstance(Locale.CHINA);
        currentYear = instance.get(Calendar.YEAR);
        currentMonth = instance.get(Calendar.MONTH)+1;
        currentTitle.set(String.valueOf(currentYear));
    }

    public void onClose(){
        dismiss();
    }

    public void onLast(){
        if(currentYear<=2000) return;
        currentYear--;
        currentTitle.set(String.valueOf(currentYear));
    }

    public void onNext(){
        currentYear++;
        currentTitle.set(String.valueOf(currentYear));
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.tv_m1:
                currentMonth = 1;
                break;
            case R.id.tv_m2:
                currentMonth = 2;
                break;
            case R.id.tv_m3:
                currentMonth = 3;
                break;
            case R.id.tv_m4:
                currentMonth = 4;
                break;
            case R.id.tv_m5:
                currentMonth = 5;
                break;
            case R.id.tv_m6:
                currentMonth = 6;
                break;
            case R.id.tv_m7:
                currentMonth = 7;
                break;
            case R.id.tv_m8:
                currentMonth = 8;
                break;
            case R.id.tv_m9:
                currentMonth = 9;
                break;
            case R.id.tv_m10:
                currentMonth = 10;
                break;
            case R.id.tv_m11:
                currentMonth = 11;
                break;
            case R.id.tv_m12:
                currentMonth = 12;
                break;
        }
        if(onOkListener!=null){
            onOkListener.onOk(currentYear,currentMonth);
        }
        dismiss();
    }

    public interface OnOkListener{
        void onOk(int year,int month);
    }
}
